﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace YJKInspectModel.PerformanceAnalysis.ShapeCoeffiecientOfBuilding
{
	internal class ShapeCoefficientOfBuildingCal
	{
		public double Volume { get; set; }

		public double Area { get; set; }

		public double ShapeCoefficient { get; set; }

		public ShapeCoefficientOfBuildingCal(StandardSegmentMaker standardSegMaker)
		{
			this.m_standardSegMaker = standardSegMaker;
			this.m_segmentAreaList = new List<SegmentAreaStruct>();
			this.m_segmentGirthList = new List<SegmentGirthStruct>();
			this.Volume = 0.0;
			this.Area = 0.0;
			this.ShapeCoefficient = 0.0;
		}

		private bool CalStandardSegmentArea()
		{
			foreach (StandardSegmentStruct standardSegmentStruct in this.m_standardSegMaker.StandardLevelSegmentList)
			{
				double area = Utility.GetArea(standardSegmentStruct.Loops);
				SegmentAreaStruct item = new SegmentAreaStruct(standardSegmentStruct.ButtomLevel, standardSegmentStruct.TopLevel, area);
				this.m_segmentAreaList.Add(item);
			}
			return true;
		}

		private bool CalStandardSegmentGirth()
		{
			foreach (StandardSegmentStruct standardSegmentStruct in this.m_standardSegMaker.StandardLevelSegmentList)
			{
				double girth = Utility.GetGirth(standardSegmentStruct.Loops);
				SegmentGirthStruct item = new SegmentGirthStruct(standardSegmentStruct.ButtomLevel, standardSegmentStruct.TopLevel, girth);
				this.m_segmentGirthList.Add(item);
			}
			return true;
		}

		private bool CalVol()
		{
			double num = 0.0;
			foreach (SegmentAreaStruct segmentAreaStruct in this.m_segmentAreaList)
			{
				double num2 = segmentAreaStruct.TopLevel - segmentAreaStruct.ButtomLevel;
				double num3 = segmentAreaStruct.Area * num2;
				num += num3;
			}
			this.Volume = num;
			return true;
		}

		private bool CalArea()
		{
            var list2 = from k in this.m_segmentGirthList
			orderby k.ButtomLevel
                                  select k;

            m_segmentGirthList.Clear();
            foreach (SegmentGirthStruct segmentGirthStruct in list2)
			{
                m_segmentGirthList.Add(segmentGirthStruct);
            }
            
			double num = 0.0;
			foreach (SegmentGirthStruct segmentGirthStruct in this.m_segmentGirthList)
			{
				double num2 = segmentGirthStruct.TopLevel - segmentGirthStruct.ButtomLevel;
				double girth = segmentGirthStruct.Girth;
				num += num2 * girth;
			}
			double num3 = 0.0;
			for (int i = 0; i < this.m_segmentAreaList.Count; i++)
			{
				double area;
				if (i == this.m_segmentAreaList.Count - 1)
				{
					area = this.m_segmentAreaList[i].Area;
					num3 += area;
					break;
				}
				double area2 = this.m_segmentAreaList[i].Area;
				area = this.m_segmentAreaList[i + 1].Area;
				num3 += Math.Abs(area2 - area);
			}
			this.Area = num3 + num;
			return true;
		}

		public bool CalShapeCoefficientOfBuilding()
		{
			this.CalStandardSegmentArea();
			this.CalStandardSegmentGirth();
			this.CalVol();
			this.CalArea();
			if (this.Area == 0.0)
			{
				return false;
			}
			this.ShapeCoefficient = this.Area / this.Volume * this.m_inchToM;
			return true;
		}

		private double m_inchToM = 3.280839895013123;

		private StandardSegmentMaker m_standardSegMaker;

		private List<SegmentAreaStruct> m_segmentAreaList;

		private List<SegmentGirthStruct> m_segmentGirthList;
	}
}
